#include <dspSimulator/command/printAReg.h>
#include <dspSimulator/dspSimulator/dspSimulator.h>


void PrintARegCommand::execute(std::string command) {
    auto dspSimulator = dynamic_cast<DSPSimulator *>(m_simulator);
    if (dspSimulator == nullptr) {
        fatalMacro("该模拟器对象不为dsp模拟器对象!");
    }
    auto targetMachine = dspSimulator->m_targetMachine;
    auto dspMachine = std::dynamic_pointer_cast<DspMachine>(targetMachine);
    if (dspMachine == nullptr) {
        fatalMacro("该目标机对象不为dsp目标机对象!");
    }

    if (command.size() < 5) {
        requestReEnter;
        return;
    }

    u32 num = stoul(command.substr(4));
    if (num >= static_cast<u32>(RegAKind::NUM_OF_Reg_A)) {
        fprintf(stdout, "不存在的寄存器A%d\n", num);
    }
    fprintf(stdout, "%s : %08X\n", RegAName[num],
            *dspMachine->getRegisters()->getReg32ByName(RegAName[num])->getDataPtr());
}

std::shared_ptr<PrintARegCommand> PrintARegCommand::get(Simulator* dspSimulator) {
    static std::shared_ptr<PrintARegCommand> printARegCommand;
    if (printARegCommand == nullptr) {
        printARegCommand = std::make_shared<PrintARegCommand>(dspSimulator);
    }
    return printARegCommand;
}
